package com.cxh.rmsq.ui.activity.self

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import androidx.core.view.isVisible
import com.aleyn.mvvm.utils.ActivityCollector
import com.cxh.rmsq.R
import com.cxh.rmsq.databinding.ActivitySetUnderPwdBinding
import com.cxh.rmsq.ui.activity.MainActivity
import com.cxh.rmsq.ui.activity.TitleBaseActivity
import com.cxh.rmsq.utils.ToastUtils
import com.xuexiang.xui.widget.edittext.verify.VerifyCodeEditText.OnInputListener
import io.rong.callkit.util.SPUtils

/**
 * 设置未成年模式密码
 */
class SetUnderPwdActivity : TitleBaseActivity() {
    private lateinit var binding: ActivitySetUnderPwdBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySetUnderPwdBinding.inflate(layoutInflater)
        setContentView(binding.root)
        titleBar.tvTitle.text = ""

        if (TextUtils.isEmpty(SPUtils.get(this, "UNDER_PWD", "").toString())) {
            binding.etCode.isVisible = true
            binding.etCodeConfirm.isVisible = false
            binding.stvOpen.isVisible = false
            binding.tvPwdForget.isVisible = false
            binding.tvText.text = "设置监管密码"

            binding.etCode.setOnInputListener(object : OnInputListener {
                override fun onComplete(input: String) {
                    binding.etCode.isVisible = false
                    binding.etCodeConfirm.isVisible = true
                    binding.stvOpen.isVisible = true
                    binding.tvText.text = "确认监管密码"
                    binding.etCodeConfirm.requestFocus()
                }

                override fun onChange(input: String) {}
                override fun onClear() {}
            })
            binding.etCodeConfirm.setOnInputListener(object : OnInputListener {
                override fun onComplete(input: String) {
                    if (input == binding.etCode.inputValue.toString()) {
                        binding.stvOpen.isEnabled = true
                        binding.stvOpen.shapeDrawableBuilder.setSolidColor(resources.getColor(R.color.color_333333))
                            .setStrokeSize(0)
                            .intoBackground()
                    } else {
//                        binding.etCodeConfirm.clearInputValue()
                        binding.stvOpen.isEnabled = false
                        ToastUtils.showToast("两次密码不一致")
                        binding.stvOpen.shapeDrawableBuilder.setSolidColor(resources.getColor(R.color.color_999999))
                            .setStrokeSize(0)
                            .intoBackground()
                    }

                }

                override fun onChange(input: String) {
                    if (input == binding.etCode.inputValue.toString()) {
                        binding.stvOpen.isEnabled = true
                        binding.stvOpen.shapeDrawableBuilder.setSolidColor(resources.getColor(R.color.color_333333))
                            .setStrokeSize(0)
                            .intoBackground()
                    } else {
                        binding.stvOpen.isEnabled = false
                        binding.stvOpen.shapeDrawableBuilder.setSolidColor(resources.getColor(R.color.color_999999))
                            .setStrokeSize(0)
                            .intoBackground()
                    }
                }

                override fun onClear() {}
            })

        } else {
            binding.tvText.text = "关闭未成年模式"
            binding.tvPwdText.text = "请输入之前设置的关闭密码"
            binding.tvPwdForget.isVisible = true
            binding.etCode.isVisible = true
            binding.etCodeConfirm.isVisible = false
            binding.stvOpen.isVisible = false

            binding.etCode.setOnInputListener(object : OnInputListener {
                override fun onComplete(input: String) {
                    if (input == SPUtils.get(this@SetUnderPwdActivity, "UNDER_PWD", "")
                            .toString()
                    ) {
                        SPUtils.put(this@SetUnderPwdActivity, "UNDER_PWD", "")
                        ToastUtils.showToast("未成年模式已关闭")
                        ActivityCollector.finishActivity(UnderModeActivity::class.java)
                        finish()
                        startActivity(Intent(this@SetUnderPwdActivity, MainActivity::class.java))
                    } else {
                        ToastUtils.showToast("密码输入不正确")
                    }
                }

                override fun onChange(input: String) {}
                override fun onClear() {}
            })

            binding.tvPwdForget.setOnClickListener {
                SPUtils.put(this, "UNDER_PWD", "")
                ToastUtils.showToast("未成年模式已关闭")
                ActivityCollector.finishActivity(UnderModeActivity::class.java)
                finish()
                startActivity(Intent(this@SetUnderPwdActivity, MainActivity::class.java))
            }
        }

        binding.stvOpen.setOnClickListener {
            SPUtils.put(this, "UNDER_PWD", binding.etCodeConfirm.inputValue.toString())
            ToastUtils.showToast("未成年模式已开启")
            finish()
        }
    }
}